{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"../artworks/matchzoo-logo.png\" alt=\"logo\" style=\"width:600px;float: center\"/>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MatchZoo Quick Start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.074291Z",
     "start_time": "2019-04-03T09:55:17.600013Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.1.0\n"
     ]
    }
   ],
   "source": [
    "import matchzoo as mz\n",
    "print(mz.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define Task"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two types of tasks available in MatchZoo. `mz.tasks.Ranking` and `mz.tasks.Classification`. We will use a ranking task for this demo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.084876Z",
     "start_time": "2019-04-03T09:55:20.076831Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ranking Task\n"
     ]
    }
   ],
   "source": [
    "task = mz.tasks.Ranking()\n",
    "print(task)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.118794Z",
     "start_time": "2019-04-03T09:55:20.087416Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_raw = mz.datasets.toy.load_data(stage='train', task=task)\n",
    "test_raw = mz.datasets.toy.load_data(stage='test', task=task)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.135138Z",
     "start_time": "2019-04-03T09:55:20.123222Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matchzoo.data_pack.data_pack.DataPack"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(train_raw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`DataPack` is a MatchZoo native data structure that most MatchZoo data handling processes build upon. A `DataPack` is consists of three `pandas.DataFrame`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.150774Z",
     "start_time": "2019-04-03T09:55:20.138602Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text_left</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id_left</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Q1</th>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q2</th>\n",
       "      <td>How are the directions of the velocity and for...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q5</th>\n",
       "      <td>how did apollo creed die</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q6</th>\n",
       "      <td>how long is the term for federal judges</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q7</th>\n",
       "      <td>how a beretta model 21 pistols magazines works</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 text_left\n",
       "id_left                                                   \n",
       "Q1                           how are glacier caves formed?\n",
       "Q2       How are the directions of the velocity and for...\n",
       "Q5                                how did apollo creed die\n",
       "Q6                 how long is the term for federal judges\n",
       "Q7          how a beretta model 21 pistols magazines works"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_raw.left.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.174204Z",
     "start_time": "2019-04-03T09:55:20.152439Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text_right</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id_right</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>D1-0</th>\n",
       "      <td>A partly submerged glacier cave on Perito More...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1-1</th>\n",
       "      <td>The ice facade is approximately 60 m high</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1-2</th>\n",
       "      <td>Ice formations in the Titlis glacier cave</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1-3</th>\n",
       "      <td>A glacier cave is a cave formed within the ice...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D1-4</th>\n",
       "      <td>Glacier caves are often called ice caves , but...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 text_right\n",
       "id_right                                                   \n",
       "D1-0      A partly submerged glacier cave on Perito More...\n",
       "D1-1              The ice facade is approximately 60 m high\n",
       "D1-2              Ice formations in the Titlis glacier cave\n",
       "D1-3      A glacier cave is a cave formed within the ice...\n",
       "D1-4      Glacier caves are often called ice caves , but..."
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_raw.right.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.183632Z",
     "start_time": "2019-04-03T09:55:20.175982Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id_left</th>\n",
       "      <th>id_right</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Q1</td>\n",
       "      <td>D1-0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Q1</td>\n",
       "      <td>D1-1</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Q1</td>\n",
       "      <td>D1-2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Q1</td>\n",
       "      <td>D1-3</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Q1</td>\n",
       "      <td>D1-4</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  id_left id_right  label\n",
       "0      Q1     D1-0    0.0\n",
       "1      Q1     D1-1    0.0\n",
       "2      Q1     D1-2    0.0\n",
       "3      Q1     D1-3    1.0\n",
       "4      Q1     D1-4    0.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_raw.relation.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is also possible to convert a `DataPack` into a single `pandas.DataFrame` that holds all information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.206192Z",
     "start_time": "2019-04-03T09:55:20.186721Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id_left</th>\n",
       "      <th>text_left</th>\n",
       "      <th>id_right</th>\n",
       "      <th>text_right</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Q1</td>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "      <td>D1-0</td>\n",
       "      <td>A partly submerged glacier cave on Perito More...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Q1</td>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "      <td>D1-1</td>\n",
       "      <td>The ice facade is approximately 60 m high</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Q1</td>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "      <td>D1-2</td>\n",
       "      <td>Ice formations in the Titlis glacier cave</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Q1</td>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "      <td>D1-3</td>\n",
       "      <td>A glacier cave is a cave formed within the ice...</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Q1</td>\n",
       "      <td>how are glacier caves formed?</td>\n",
       "      <td>D1-4</td>\n",
       "      <td>Glacier caves are often called ice caves , but...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  id_left                      text_left id_right  \\\n",
       "0      Q1  how are glacier caves formed?     D1-0   \n",
       "1      Q1  how are glacier caves formed?     D1-1   \n",
       "2      Q1  how are glacier caves formed?     D1-2   \n",
       "3      Q1  how are glacier caves formed?     D1-3   \n",
       "4      Q1  how are glacier caves formed?     D1-4   \n",
       "\n",
       "                                          text_right  label  \n",
       "0  A partly submerged glacier cave on Perito More...    0.0  \n",
       "1          The ice facade is approximately 60 m high    0.0  \n",
       "2          Ice formations in the Titlis glacier cave    0.0  \n",
       "3  A glacier cave is a cave formed within the ice...    1.0  \n",
       "4  Glacier caves are often called ice caves , but...    0.0  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_raw.frame().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, using such `pandas.DataFrame` consumes much more memory if there are many duplicates in the texts, and that is the exact reason why we use `DataPack`. For more details about data handling, consult `matchzoo/tutorials/data_handling.ipynb`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preprocessing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MatchZoo preprocessors are used to convert a raw `DataPack` into a `DataPack` that ready to be fed into a model. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.210082Z",
     "start_time": "2019-04-03T09:55:20.207488Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "preprocessor = mz.preprocessors.BasicPreprocessor()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are two steps to use a preprocessor. First, `fit`. Then, `transform`. `fit` will only changes the preprocessor's inner state but not the input `DataPack`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.273214Z",
     "start_time": "2019-04-03T09:55:20.212140Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing text_left with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 13/13 [00:00<00:00, 1194.41it/s]\n",
      "Processing text_right with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 100/100 [00:00<00:00, 4426.94it/s]\n",
      "Processing text_right with append: 100%|██████████| 100/100 [00:00<00:00, 160516.80it/s]\n",
      "Building FrequencyFilter from a datapack.: 100%|██████████| 100/100 [00:00<00:00, 69742.33it/s]\n",
      "Processing text_right with transform: 100%|██████████| 100/100 [00:00<00:00, 96067.43it/s]\n",
      "Processing text_left with extend: 100%|██████████| 13/13 [00:00<00:00, 14364.05it/s]\n",
      "Processing text_right with extend: 100%|██████████| 100/100 [00:00<00:00, 129854.61it/s]\n",
      "Building Vocabulary from a datapack.: 100%|██████████| 1665/1665 [00:00<00:00, 1703712.16it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matchzoo.preprocessors.basic_preprocessor.BasicPreprocessor at 0x1333081d0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocessor.fit(train_raw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fit` will gather useful information into its `context`, which will be used later in a `transform` or used to set hyper-parameters of a model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.278189Z",
     "start_time": "2019-04-03T09:55:20.274519Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'filter_unit': <matchzoo.preprocessors.units.frequency_filter.FrequencyFilter at 0x133308a20>,\n",
       " 'vocab_unit': <matchzoo.preprocessors.units.vocabulary.Vocabulary at 0x133515cf8>,\n",
       " 'vocab_size': 285,\n",
       " 'embedding_input_dim': 285,\n",
       " 'input_shapes': [(30,), (30,)]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preprocessor.context"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once `fit`, the preprocessor has enough information to `transform`.  `transform` will not change the preprocessor's inner state and the input `DataPack`, but return a transformed `DataPack`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.369369Z",
     "start_time": "2019-04-03T09:55:20.280596Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing text_left with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 13/13 [00:00<00:00, 6229.40it/s]\n",
      "Processing text_right with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 100/100 [00:00<00:00, 4721.45it/s]\n",
      "Processing text_right with transform: 100%|██████████| 100/100 [00:00<00:00, 38168.20it/s]\n",
      "Processing text_left with transform: 100%|██████████| 13/13 [00:00<00:00, 20127.70it/s]\n",
      "Processing text_right with transform: 100%|██████████| 100/100 [00:00<00:00, 106158.04it/s]\n",
      "Processing length_left with len: 100%|██████████| 13/13 [00:00<00:00, 20568.07it/s]\n",
      "Processing length_right with len: 100%|██████████| 100/100 [00:00<00:00, 146398.05it/s]\n",
      "Processing text_left with transform: 100%|██████████| 13/13 [00:00<00:00, 24954.67it/s]\n",
      "Processing text_right with transform: 100%|██████████| 100/100 [00:00<00:00, 66010.45it/s]\n",
      "Processing text_left with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 3/3 [00:00<00:00, 1892.74it/s]\n",
      "Processing text_right with chain_transform of Tokenize => Lowercase => PuncRemoval: 100%|██████████| 20/20 [00:00<00:00, 3610.80it/s]\n",
      "Processing text_right with transform: 100%|██████████| 20/20 [00:00<00:00, 32948.19it/s]\n",
      "Processing text_left with transform: 100%|██████████| 3/3 [00:00<00:00, 6275.77it/s]\n",
      "Processing text_right with transform: 100%|██████████| 20/20 [00:00<00:00, 35833.44it/s]\n",
      "Processing length_left with len: 100%|██████████| 3/3 [00:00<00:00, 1872.74it/s]\n",
      "Processing length_right with len: 100%|██████████| 20/20 [00:00<00:00, 36776.01it/s]\n",
      "Processing text_left with transform: 100%|██████████| 3/3 [00:00<00:00, 3333.22it/s]\n",
      "Processing text_right with transform: 100%|██████████| 20/20 [00:00<00:00, 23838.04it/s]\n"
     ]
    }
   ],
   "source": [
    "train_processed = preprocessor.transform(train_raw)\n",
    "test_processed = preprocessor.transform(test_raw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.383522Z",
     "start_time": "2019-04-03T09:55:20.371811Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>text_left</th>\n",
       "      <th>length_left</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id_left</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Q1</th>\n",
       "      <td>[263, 117, 232, 112, 21, 0, 0, 0, 0, 0, 0, 0, ...</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q2</th>\n",
       "      <td>[263, 117, 89, 194, 22, 89, 225, 186, 195, 105...</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q5</th>\n",
       "      <td>[263, 275, 268, 236, 158, 0, 0, 0, 0, 0, 0, 0,...</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q6</th>\n",
       "      <td>[263, 101, 157, 89, 50, 37, 274, 141, 0, 0, 0,...</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Q7</th>\n",
       "      <td>[263, 102, 63, 58, 164, 3, 38, 222, 0, 0, 0, 0...</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 text_left  length_left\n",
       "id_left                                                                \n",
       "Q1       [263, 117, 232, 112, 21, 0, 0, 0, 0, 0, 0, 0, ...            5\n",
       "Q2       [263, 117, 89, 194, 22, 89, 225, 186, 195, 105...           15\n",
       "Q5       [263, 275, 268, 236, 158, 0, 0, 0, 0, 0, 0, 0,...            5\n",
       "Q6       [263, 101, 157, 89, 50, 37, 274, 141, 0, 0, 0,...            8\n",
       "Q7       [263, 102, 63, 58, 164, 3, 38, 222, 0, 0, 0, 0...            8"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_processed.left.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we can see, `text_left` is already in sequence form that nerual networks love."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Just to make sure we have the correct sequence:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.390512Z",
     "start_time": "2019-04-03T09:55:20.385506Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Orig Text: [263, 117, 232, 112, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
      "Transformed Indices: [263, 117, 232, 112, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
      "Transformed Indices Meaning: how_are_glacier_caves_formed_________________________\n"
     ]
    }
   ],
   "source": [
    "vocab_unit = preprocessor.context['vocab_unit']\n",
    "print('Orig Text:', train_processed.left.loc['Q1']['text_left'])\n",
    "sequence = train_processed.left.loc['Q1']['text_left']\n",
    "print('Transformed Indices:', sequence)\n",
    "print('Transformed Indices Meaning:',\n",
    "      '_'.join([vocab_unit.state['index_term'][i] for i in sequence]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For more details about preprocessing, consult `matchzoo/tutorials/data_handling.ipynb`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Build Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MatchZoo provides many built-in text matching models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.396497Z",
     "start_time": "2019-04-03T09:55:20.392055Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[matchzoo.models.naive.Naive,\n",
       " matchzoo.models.dssm.DSSM,\n",
       " matchzoo.models.cdssm.CDSSM,\n",
       " matchzoo.models.dense_baseline.DenseBaseline,\n",
       " matchzoo.models.arci.ArcI,\n",
       " matchzoo.models.arcii.ArcII,\n",
       " matchzoo.models.match_pyramid.MatchPyramid,\n",
       " matchzoo.models.knrm.KNRM,\n",
       " matchzoo.models.duet.DUET,\n",
       " matchzoo.models.drmmtks.DRMMTKS,\n",
       " matchzoo.models.drmm.DRMM,\n",
       " matchzoo.models.anmm.ANMM,\n",
       " matchzoo.models.mvlstm.MVLSTM,\n",
       " matchzoo.contrib.models.match_lstm.MatchLSTM,\n",
       " matchzoo.models.conv_knrm.ConvKNRM]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mz.models.list_available()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's use `mz.models.DenseBaseline` for our demo."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.403308Z",
     "start_time": "2019-04-03T09:55:20.399279Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model = mz.models.DenseBaseline()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model is initialized with a hyper parameter table, in which values are partially filled. To view parameters and their values, use `print`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.408216Z",
     "start_time": "2019-04-03T09:55:20.405224Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model_class                   <class 'matchzoo.models.dense_baseline.DenseBaseline'>\n",
      "input_shapes                  None\n",
      "task                          None\n",
      "optimizer                     adam\n",
      "with_multi_layer_perceptron   True\n",
      "mlp_num_units                 256\n",
      "mlp_num_layers                3\n",
      "mlp_num_fan_out               64\n",
      "mlp_activation_func           relu\n"
     ]
    }
   ],
   "source": [
    "print(model.params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`to_frame` gives you more informartion in addition to just names and values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.431258Z",
     "start_time": "2019-04-03T09:55:20.420657Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "      <th>Description</th>\n",
       "      <th>Value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>model_class</td>\n",
       "      <td>Model class. Used internally for save/load. Ch...</td>\n",
       "      <td>&lt;class 'matchzoo.models.dense_baseline.DenseBa...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>input_shapes</td>\n",
       "      <td>Dependent on the model and data. Should be set...</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>task</td>\n",
       "      <td>Decides model output shape, loss, and metrics.</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>optimizer</td>\n",
       "      <td>None</td>\n",
       "      <td>adam</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>with_multi_layer_perceptron</td>\n",
       "      <td>A flag of whether a multiple layer perceptron ...</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mlp_num_units</td>\n",
       "      <td>Number of units in first `mlp_num_layers` layers.</td>\n",
       "      <td>256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mlp_num_layers</td>\n",
       "      <td>Number of layers of the multiple layer percetron.</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mlp_num_fan_out</td>\n",
       "      <td>Number of units of the layer that connects the...</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mlp_activation_func</td>\n",
       "      <td>Activation function used in the multiple layer...</td>\n",
       "      <td>relu</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          Name  \\\n",
       "0                  model_class   \n",
       "1                 input_shapes   \n",
       "2                         task   \n",
       "3                    optimizer   \n",
       "4  with_multi_layer_perceptron   \n",
       "5                mlp_num_units   \n",
       "6               mlp_num_layers   \n",
       "7              mlp_num_fan_out   \n",
       "8          mlp_activation_func   \n",
       "\n",
       "                                         Description  \\\n",
       "0  Model class. Used internally for save/load. Ch...   \n",
       "1  Dependent on the model and data. Should be set...   \n",
       "2     Decides model output shape, loss, and metrics.   \n",
       "3                                               None   \n",
       "4  A flag of whether a multiple layer perceptron ...   \n",
       "5  Number of units in first `mlp_num_layers` layers.   \n",
       "6  Number of layers of the multiple layer percetron.   \n",
       "7  Number of units of the layer that connects the...   \n",
       "8  Activation function used in the multiple layer...   \n",
       "\n",
       "                                               Value  \n",
       "0  <class 'matchzoo.models.dense_baseline.DenseBa...  \n",
       "1                                               None  \n",
       "2                                               None  \n",
       "3                                               adam  \n",
       "4                                               True  \n",
       "5                                                256  \n",
       "6                                                  3  \n",
       "7                                                 64  \n",
       "8                                               relu  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.params.to_frame()[['Name', 'Description', 'Value']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To set a hyper-parameter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.444053Z",
     "start_time": "2019-04-03T09:55:20.439641Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model_class                   <class 'matchzoo.models.dense_baseline.DenseBaseline'>\n",
      "input_shapes                  None\n",
      "task                          Ranking Task\n",
      "optimizer                     adam\n",
      "with_multi_layer_perceptron   True\n",
      "mlp_num_units                 3\n",
      "mlp_num_layers                3\n",
      "mlp_num_fan_out               64\n",
      "mlp_activation_func           relu\n"
     ]
    }
   ],
   "source": [
    "model.params['task'] = task\n",
    "model.params['mlp_num_units'] = 3\n",
    "print(model.params)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that we are still missing `input_shapes`, and that information is store in the preprocessor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.448541Z",
     "start_time": "2019-04-03T09:55:20.445714Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(30,), (30,)]\n"
     ]
    }
   ],
   "source": [
    "print(preprocessor.context['input_shapes'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We may use `update` to load a preprocessor's context into a model's hyper-parameter table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.453045Z",
     "start_time": "2019-04-03T09:55:20.450104Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.params.update(preprocessor.context)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we have a completed hyper-parameter table."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.458269Z",
     "start_time": "2019-04-03T09:55:20.454854Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.params.completed()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With all parameters filled in, we can now build and compile the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.563119Z",
     "start_time": "2019-04-03T09:55:20.459804Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.build()\n",
    "model.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MatchZoo models are wrapped over keras models, and the `backend` property of a model gives you the actual keras model built."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.571750Z",
     "start_time": "2019-04-03T09:55:20.564862Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__________________________________________________________________________________________________\n",
      "Layer (type)                    Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      "text_left (InputLayer)          (None, 30)           0                                            \n",
      "__________________________________________________________________________________________________\n",
      "text_right (InputLayer)         (None, 30)           0                                            \n",
      "__________________________________________________________________________________________________\n",
      "concatenate_1 (Concatenate)     (None, 60)           0           text_left[0][0]                  \n",
      "                                                                 text_right[0][0]                 \n",
      "__________________________________________________________________________________________________\n",
      "dense_1 (Dense)                 (None, 3)            183         concatenate_1[0][0]              \n",
      "__________________________________________________________________________________________________\n",
      "dense_2 (Dense)                 (None, 3)            12          dense_1[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "dense_3 (Dense)                 (None, 3)            12          dense_2[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "dense_4 (Dense)                 (None, 64)           256         dense_3[0][0]                    \n",
      "__________________________________________________________________________________________________\n",
      "dense_5 (Dense)                 (None, 1)            65          dense_4[0][0]                    \n",
      "==================================================================================================\n",
      "Total params: 528\n",
      "Trainable params: 528\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.backend.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-12-10T13:28:54.044570Z",
     "start_time": "2018-12-10T13:28:54.038325Z"
    }
   },
   "source": [
    "For more details about models, consult `matchzoo/tutorials/models.ipynb`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train, Evaluate, Predict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A `DataPack` can `unpack` itself into data that can be directly used to train a MatchZoo model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:20.605059Z",
     "start_time": "2019-04-03T09:55:20.574058Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x, y = train_processed.unpack()\n",
    "test_x, test_y = test_processed.unpack()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:21.281367Z",
     "start_time": "2019-04-03T09:55:20.606828Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "100/100 [==============================] - 0s 3ms/step - loss: 2.7378\n",
      "Epoch 2/5\n",
      "100/100 [==============================] - 0s 63us/step - loss: 0.3293\n",
      "Epoch 3/5\n",
      "100/100 [==============================] - 0s 58us/step - loss: 0.3964\n",
      "Epoch 4/5\n",
      "100/100 [==============================] - 0s 60us/step - loss: 0.2228\n",
      "Epoch 5/5\n",
      "100/100 [==============================] - 0s 69us/step - loss: 0.1432\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x133584b38>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x, y, batch_size=32, epochs=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An alternative to train a model is to use a `DataGenerator`. This is useful for delaying expensive preprocessing steps or doing real-time data augmentation. For some models that needs dynamic batch-wise information, using a `DataGenerator` is required. For more details about `DataGenerator`, consult `matchzoo/tutorials/data_handling.ipynb`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:21.285679Z",
     "start_time": "2019-04-03T09:55:21.282881Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data_generator = mz.DataGenerator(train_processed, batch_size=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:21.944886Z",
     "start_time": "2019-04-03T09:55:21.287913Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "4/4 [==============================] - 0s 14ms/step - loss: 0.1749\n",
      "Epoch 2/5\n",
      "4/4 [==============================] - 0s 32ms/step - loss: 0.1149\n",
      "Epoch 3/5\n",
      "4/4 [==============================] - 0s 30ms/step - loss: 0.0773\n",
      "Epoch 4/5\n",
      "4/4 [==============================] - 0s 31ms/step - loss: 0.0625\n",
      "Epoch 5/5\n",
      "4/4 [==============================] - 0s 30ms/step - loss: 0.0984\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x133bd15f8>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit_generator(data_generator, epochs=5, use_multiprocessing=True, workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:21.996760Z",
     "start_time": "2019-04-03T09:55:21.947722Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{mean_average_precision(0.0): 0.16666666666666666}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_x, test_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:55:22.005670Z",
     "start_time": "2019-04-03T09:55:21.998800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.00927439],\n",
       "       [ 0.06645402],\n",
       "       [ 0.00299546],\n",
       "       [ 0.06593451],\n",
       "       [ 0.19827756],\n",
       "       [-0.00519839],\n",
       "       [-0.04881426],\n",
       "       [-0.07771388],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.07235113],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.04881426],\n",
       "       [-0.08091632]], dtype=float32)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.predict(test_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-01-27T15:38:33.485790Z",
     "start_time": "2019-01-27T15:38:33.483430Z"
    }
   },
   "source": [
    "## A Shortcut to Preprocessing and Model Building"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since data preprocessing and model building are laborious and special setups of some models makes this even worse, MatchZoo provides `prepare`, a unified interface that handles interaction among data, model, and preprocessor automatically.\n",
    "\n",
    "More specifically, `prepare` does these following things:\n",
    " - create a default preprocessor of the model class (if not given one)\n",
    " - fit the preprocessor using the raw data\n",
    " - create an embedding matrix\n",
    " - instantiate a model and fill in hype-parameters\n",
    " - build the model\n",
    " - instantiate a `DataGeneratorBuilder` that will build a correctly formed `DataGenerator` given a `DataPack`\n",
    " \n",
    "It also does many special handling for specific models, but we will not go into the details of that here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:56:29.684941Z",
     "start_time": "2019-04-03T09:55:22.008020Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'matchzoo.models.naive.Naive'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 0s 139ms/step - loss: 62835.0703\n",
      "\n",
      "<class 'matchzoo.models.dssm.DSSM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 556ms/step - loss: 0.0507\n",
      "\n",
      "<class 'matchzoo.models.cdssm.CDSSM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 2s 2s/step - loss: 0.1703\n",
      "\n",
      "<class 'matchzoo.models.dense_baseline.DenseBaseline'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 0s 458ms/step - loss: 259.2346\n",
      "\n",
      "<class 'matchzoo.models.arci.ArcI'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 617ms/step - loss: 0.0480\n",
      "\n",
      "<class 'matchzoo.models.arcii.ArcII'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 717ms/step - loss: 0.0546\n",
      "\n",
      "<class 'matchzoo.models.match_pyramid.MatchPyramid'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 559ms/step - loss: 0.0518\n",
      "\n",
      "<class 'matchzoo.models.knrm.KNRM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 1s/step - loss: 9780.9414\n",
      "\n",
      "<class 'matchzoo.models.duet.DUET'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 1s/step - loss: 1.4414\n",
      "\n",
      "<class 'matchzoo.models.drmmtks.DRMMTKS'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 1s/step - loss: 0.0691\n",
      "\n",
      "<class 'matchzoo.models.drmm.DRMM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 1s/step - loss: 0.0770\n",
      "\n",
      "<class 'matchzoo.models.anmm.ANMM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 1s 953ms/step - loss: 0.0463\n",
      "\n",
      "<class 'matchzoo.models.mvlstm.MVLSTM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 3s 3s/step - loss: 0.0487\n",
      "\n",
      "<class 'matchzoo.contrib.models.match_lstm.MatchLSTM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 5s 5s/step - loss: 0.0475\n",
      "\n",
      "<class 'matchzoo.models.conv_knrm.ConvKNRM'>\n",
      "Epoch 1/1\n",
      "1/1 [==============================] - 7s 7s/step - loss: 540.5453\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for model_class in mz.models.list_available():\n",
    "    print(model_class)\n",
    "    model, preprocessor, data_generator_builder, embedding_matrix = mz.auto.prepare(\n",
    "        task=task,\n",
    "        model_class=model_class,\n",
    "        data_pack=train_raw,\n",
    "    )\n",
    "    train_processed = preprocessor.transform(train_raw, verbose=0)\n",
    "    test_processed = preprocessor.transform(test_raw, verbose=0)\n",
    "    train_gen = data_generator_builder.build(train_processed)\n",
    "    test_gen = data_generator_builder.build(test_processed)\n",
    "    model.fit_generator(train_gen, epochs=1)\n",
    "    model.evaluate_generator(test_gen)\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save and Load the Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-04-03T09:56:35.125254Z",
     "start_time": "2019-04-03T09:56:29.686705Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.save('my-model')\n",
    "loaded_model = mz.load_model('my-model')"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "matchzoo",
   "language": "python",
   "name": "matchzoo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
